package com.cqggrw.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.cqggrw.dao.FlashDaoI;
import com.cqggrw.dao.impl.FlashDaoImpl;
import com.cqggrw.domain.Flash;
import com.cqggrw.util.Utils;

public class FlashserviceImpl implements com.cqggrw.service.FlashserviceI {

	private FlashDaoI dao = new FlashDaoImpl(); 
	
	public boolean add(Flash flash, String realPath) {
		String imgUrl = "";
		if(flash.getImg() != null) {
			imgUrl = UUID.randomUUID().toString() +"." + flash.getImgFileName().replace(".", ",").split(",")[1];
			try {
				Utils.upFile(imgUrl, realPath + java.io.File.separator+"images"+java.io.File.separator+"FlashImg"+java.io.File.separator, flash.getImg());
			} catch (IOException e) {
				Utils.log(e);
			}
		}
		flash.setImgUrl(imgUrl);
		return dao.add(flash);
	}

	public boolean del(Integer id) {
		return dao.del(id);
	}

	public boolean modify(Flash flash, String realPath) {
		String separator = java.io.File.separator;
		String imgUrl = "";
		if(flash.getImg() != null) {
			imgUrl = UUID.randomUUID().toString() +"." + flash.getImgFileName().replace(".", ",").split(",")[1];
			try {
				Utils.upFile(imgUrl, realPath +separator + "images"+separator+"FlashImg"+separator, flash.getImg());
			} catch (IOException e) {
				e.printStackTrace();
			}
			flash.setImgUrl(imgUrl);
		}
		return dao.modify(flash);
	}

	public Flash getById(Integer id) {
		return dao.getById(id);
	}

	public Object get(Flash flash) {
		List<Flash> list = dao.get(flash);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", dao.getTotal());
		map.put("rows", list);
		return map;
	}

	public List<Flash> getFlashImg(Integer i) {
		return dao.getFlashImg(i);
	}

}
